function getDate() {
	var date = new Date();
	var y = date.getFullYear(); //获取当前年份
	var m = date.getMonth() + 1;
	var d = date.getDate();
	var h = date.getHours();
	var mi = date.getMinutes();
	var s = date.getSeconds();
	return y + '-' + m + '-' + d + ' ' + h + ':' + mi + ':' + s;
}
var share = function() {
	return new Share();
}

function Share() { //构造函数

}
Share.prototype.lazy = function(obj) //图片懒加载
{
	obj.each(function() {
		if($(this).offset().top != 0) {
			if($(this).offset().top - $('.header').outerHeight() < $('.main').height() + $(this).scrollTop()) {
				if($(this).attr("src") == "img/jiazai.png") {
					$(this).attr("src", $(this).attr("xsrc"));
					$(this).animate({
						opacity: 1
					}, 200);
				}
			}
		}
	});
}
Share.prototype.size = function() {
	var w = $(window).width();
	var h = $(window).height();
	$('#wrap').css('height', h + 'px');
	$('#sideBar').css('width', w * 0.75 + 'px');
	$('#homePage').css('width', w + 'px');
}
Share.prototype.wode = function() {
	var div = $('.wode');
	div.css('bottom', $('.foot').outerHeight() + "px");
	$('#foot_span3').on('click', function() {
		if(div.is(':hidden')) {
			div.show();
		} else {
			div.hide();
		}
	})
}

Share.prototype.lanLoad = function(img, scrollE) {
	share().lazy(img);
	scrollE.scroll(function() {
		share().lazy(img);
	});
}
Share.prototype.createDiv = function(obj, div) {
	var zDiv = $('.' + obj.style);
	var a = "<div class='jianli'><div class='jianli_div_top'><div class='jianli_div_top1'><img xsrc=" + obj.src +
		" src='img/jiazai.png'  s=" + obj.style + " num=" + obj.num + " class='jianli_div_img'/></div></div><div class='jianli_div_bottom'><span><b>" + obj.id +
		"</b><i class='iconfont icon-xihuan1'></i></span><a class='jianli_div_bottom_a' href=" + obj.href + ">下载</a></div></div>";
	div.append(a);
	zDiv.append(a);
}
Share.prototype.createDivDown = function(obj, div) {
	var a = "<div class='jianli'><div class='jianli_div_top'><div class='jianli_div_top1'><img xsrc=" + obj.src +
		" src='img/jiazai.png' s=" + obj.style + " class='jianli_div_img' /></div></div><div class='jianli_div_bottom'><span><b>" + obj.id +
		"</b></span><a class='jianli_div_bottom_a' xhref=" + obj.href + " xzdz=" + obj.xzdz + ">打开</a></div></div>";
	div.append(a);
}
Share.prototype.shoucang = function(oid, table) {
	$.ajax({
		type: "post",
		url: "php/shoucang.php",
		async: true,
		data: $.param({
			user: $.cookie('username'),
			id: oid,
			biao: table
		}),
		success: function(data, status, xhr) {}
	});
}
Share.prototype.unShoucang = function(oid) {
	$.post('php/removedata.php', {
			user: $.cookie('username'),
			id: oid
		},
		function(data, status) {});
}
Share.prototype.xinRed = function(obj) {
	if(obj.hasClass('red')) {
		obj.removeClass('red');
		share().unShoucang(obj.siblings('b').html());
	} else {
		var b = obj.siblings('b').html();
		share().shoucang(b, 'shoucang');
		obj.addClass('red');
	}
}
Share.prototype.xinLoad = function() {
	$.post('php/data.php', {
			user: $.cookie('username')
		},
		function(data, status) {
			var ojson = $.parseJSON(data);
			var div = $('.jianli').find('b');
			$.each(ojson, function(index, element) {
				var that = this;
				div.each(function() {
					if(that.id == $(this).html()) {
						$(this).siblings('i').addClass('red');
					}
				});
			})
		});
}
Share.prototype.addDown = function(oid, xzdz) //存储下载数据
{
	$.post('php/xiazai.php', {
			user: $.cookie('username'),
			id: oid,
			xz: xzdz
		},
		function(data, status) {});
}
Share.prototype.clickImg = function(obj) {
	var b = obj.parents('.jianli').find('b').html();
	$('#content').css('display', 'none');
	$('#pinglun').css('display', 'block');
	$('.pl_main').scrollTop('0');
	$('.pl_main_1').css('background-image', 'url(' + obj.attr('xsrc') + ')');
	$('.pl_main_1').css('background-size', '100% 100%');
	$('.pl_main_2_top_p1').html(function() {
		return "编号:" + b;
	}).attr('b', b);
	$('.pl_main_2_top_p2').html(function() {
		var bb;
		switch(obj.attr('s')) {
			case 'tj':
				bb = '推荐';
				break;
			case 'fm':
				bb = '封面';
				break;
			case 'jj':
				bb = '简洁';
				break;
			case 'xqx':
				bb = '小清新';
				break;
			case 'cy':
				bb = '创意';
				break;
		}
		return "风格:" + bb;
	});
	$('.num').html(obj.attr('num'));
	share().showPl(b);
}
Share.prototype.showPl = function(oid) {
	$.ajax({
		type: "post",
		url: "php/showPL.php",
		async: true,
		data: $.param({
			id: oid,
		}),
		beforeSend: function() {
			$('.loading').css('display', 'block');
			$('.loading').html('评论加载中');
		},
		success: function(data, status, xhr) {
			if(status == 'success') {
				$('.loading').html('加载成功');
			}
			setTimeout(function() {
				$('.loading').css('display', 'none');
			}, 1000);
//			$(".pl_main").animate({
//				'scrollTop': 110
//			}, 100);
			var ojson = $.parseJSON(data);
			if(ojson.length == 0) {
				$('textarea').attr('placeholder', '还没有评论哟，赶紧坐第一把沙发吧');
				$('.fabiao').slideDown(100);
				$(".pl_main").animate({
					'scrollTop': 110
				}, 100);
			} else {
				$.each(ojson, function(index, element) {
					share().createDL({
						cookie: element.cookie,
						zan: element.zan,
						date: element.date,
						content: decodeURIComponent(element.content)
					}, $('.showpl'));
				})
				$('.dianzan_span_min').on('click', function() {
					var that = this;
					share().dianzan(this);
					$.post('php/xiugaipl.php', {
							id: $(that).parents('dl').contents().filter(function() {
								return this.nodeType === 3;
							}).text(),
							date: $(that).parents('dl').children('dt').html(),
							num: $(that).children('.num').html()
						},
						function(data, status) {});
				})

			}
		}
	});
}
Share.prototype.faBiao = function() {
	var oid = $('.pl_main_2_top_p1').attr('b');
	var ndate = getDate();
	var ncontent = $('textarea').val();
	if(ncontent != "") {
		$.ajax({
			type: "post",
			url: "php/pinglun.php",
			async: true,
			data: $.param({
				user: $.cookie('username'),
				id: oid,
				content: encodeURIComponent(ncontent),
				num: 0,
				date: ndate
			}),
			beforeSend: function() {
				$('.loading').css('display', 'block');
				$('.loading').html('正在提交');
			},
			success: function(data, status, xhr) {
				if(status == 'success') {
					$('.loading').html('提交成功');
					setTimeout(function() {
						$('.loading').css('display', 'none');
					}, 1000);
					$('textarea').val('');
					$('textarea').attr('placeholder', '宝贝满足你的期待吗？说说你的体验，分享与他人');
					share().createDL({ cookie: $.cookie('username'), zan: 0, date: ndate, content: ncontent }, $('.showpl'));
					$('.dianzan_span_min').eq(0).on('click', function() {
						share().dianzan(this);
						var that = this;
						$.post('php/xiugaipl.php', {
								id: $(that).parents('dl').contents().filter(function() {
									return this.nodeType === 3;
								}).text(),
								date: $(that).parents('dl').children('dt').html(),
								num: $(that).children('.num').html()
							},
							function(data, status) {});
					});
					if($(".pl_main").scrollTop() <= 120) {
						$(".pl_main").animate({
							'scrollTop': 200
						}, 100);
					}

				}

			}
		});

	}
}
Share.prototype.dianzanA = function(obj) {
	obj = $.extend({
		o: null, //jq对象，要在那个html标签上显示
		str: "+1",
		startSize: "12px",
		endSize: "18px",
		time: 600,
		color: "red",
		callback: function() {}
	}, obj); //默认传参
	$("#pinglun").append("<span class='zan'>" + obj.str + "</span>");
	var box = $(".zan");
	var left = obj.o.offset().left + obj.o.width() / 2;
	var top = obj.o.offset().top - obj.o.height();
	box.css({
		"position": "absolute",
		"left": left + "px",
		"top": top + "px",
		"z-index": 9999,
		"font-size": obj.startSize,
		"line-height": obj.endSize,
		"color": obj.color
	});
	box.animate({
		"font-size": obj.endSize,
		"opacity": "0",
		"top": top - parseInt(obj.endSize) + "px"
	}, obj.interval, function() {
		box.remove();
		obj.callback();
	});
}
Share.prototype.dianzan = function(element) {
	var that = element;
	var i = $(element).children('i');
	if(i.hasClass('red')) {
		share().dianzanA({
			o: $(that),
			str: "-1",
			color: "#000",
			callback: function() {}
		});
		i.removeClass('red');
		var n = parseInt($(element).children('.num').html()) - 1;
		$(element).children('.num').html(n);
	} else {
		share().dianzanA({
			o: $(that),
			str: "+1",
			callback: function() {}
		});
		i.addClass('red');
		$(element).children('.num').html(parseInt($(element).children('.num').html()) + 1);
	}
}
Share.prototype.createDL = function(obj, div) {
	var a = "<dl>" + obj.cookie + "<span class='dianzan_span_min'><i class='iconfont icon-dianzan1'></i><i class='num'>" + obj.zan +
		"</i></span><dt>" + obj.date + "</dt><dd>" + obj.content + "</dd></dl>";
	div.prepend(a);
}
function initSize()
{
			share().size();
		var header_h = $('.header').outerHeight();
	var foot_h = $('.foot').outerHeight();
	var h = $(window).height() - header_h - foot_h;
	var pl_h = $(window).height() - header_h;
	$('.main').css('height', h + 'px');
	if($('.nav').outerHeight() != undefined) {
		var pl_h = $(window).height() - $('.header').outerHeight() + $('.nav').outerHeight();
	} else {
		var pl_h = $(window).height() - $('.header').outerHeight();
	}
	$('.pl_main').css('height', pl_h + 'px');
	$('.pl_main_1').css('height', pl_h - 100 + 'px');
}
$(function() {
	initSize();
	share().wode();
	$(window).resize(function(){
initSize();
	})
})